<?php
/*
 * ebiz front
 *
 * ProductService Service
 *
 * @author allen
 *
 */

require_once '../app/core/services/BaseService.php';
require_once '../app/models/CategoryProductDao.php';
require_once '../app/models/ProductDao.php';
require_once '../app/models/ProductItemDao.php';
require_once '../app/models/ProductImagesDao.php';
require_once '../app/models/PromotionDao.php';

require_once '../app/constants/ConfigConstants.php';

//akimimi new add
require_once '../app/models/MetadataDao.php';
require_once '../app/models/MetadataOptionDao.php';
require_once '../app/models/MetadataGroupDao.php';
require_once '../app/models/ItemMetadataDao.php';
require_once '../app/models/QueryDao.php';
require_once '../app/models/OrderDao.php';
require_once '../app/models/GiftDao.php';

require_once '../app/models/GlobalSearchDataDao.php';

require_once '../app/views/buy_knowledge/BuyKnowledgeView.php';

class ProductService extends BaseService
{

	private $categoryProductDao;
	private $productDao;
	private $productItemDao;
	private $productImageDao;
	private $productGroupDao;
	private $promotionDao;

	private $metadataDao;
	private $metadataOptionDao;
	private $metadataGroupDao;
	private $itemMetadataDao;
	private $queryDao;
	private $orderDao;
	private $giftDao;
	
	private $globalSearchDataDao;

	public function __construct(){
		parent::__construct();
		$this->productDao = new ProductDao($this->dbUtil);
		$this->categoryProductDao = new CategoryProductDao($this->dbUtil);
		$this->productItemDao = new ProductItemDao($this->dbUtil);
		$this->productImageDao = new ProductImagesDao($this->dbUtil);
		$this->promotionDao = new PromotionDao($this->dbUtil);

		$this->metadataDao = new MetadataDao($this->dbUtil);
		$this->metadataOptionDao = new MetadataOptionDao($this->dbUtil);
		$this->metadataGroupDao = new MetadataGroupDao($this->dbUtil);
		$this->itemMetadataDao = new ItemMetadataDao($this->dbUtil);
		$this->queryDao = new QueryDao($this->dbUtil);
		$this->orderDao = new OrderDao($this->dbUtil);
		$this->giftDao = new GiftDao($this->dbUtil);
		
		$this->globalSearchDataDao = new GlobalSearchDataDao($this->dbUtil);
	}

	public function getByProductId($pid){
	   return $this->categoryProductDao->getByProductId($pid);
	}

	public function getProductJson($urlTitle, $isMobile = false){ //0629 add, delete the /o/showProductInfo ajax function
        $urlTitle = strtolower($urlTitle);
        $productService = new ProductService();
        $productInfo = array();
        $productInfo = $productService->getProductByUrlTitle($urlTitle, $isMobile);
        return json_encode($productInfo);
	}

	public function getProductIdByUrlTitle($productUrlTitle){    //return the pid used in productView
        $arr = $this->productDao->getByUrlTitle($productUrlTitle);
        $pid = $arr[0]['id'];
        $param = $this->getDefaultItemByPid($pid);
        //if the item have not default image then set the $showItemId = 0
        $showItemId = isset($param[0]['item_id']) ? $param[0]['item_id'] : 0 ;
        return $showItemId;
	}

	public function getProductByUrlTitle($productUrlTitle, $isMobile = false){   //give the product url title //used in js
        $config = $GLOBALS["config"];
        $attrNum = 0;//count how many effective attrbute
		$arrProduct = $this->productDao->getByUrlTitle($productUrlTitle);
		$pid = $arrProduct[0]['id'];
		$productTitle = $arrProduct[0]['title'];
		$arr = $this->productItemDao->getByProductId($pid);
//		$distinctAttributes = $this->productItemDao->getDistinctAttributeByProductId($pid);
		$pitems = array();
        $param = $this->getDefaultItemByPid($pid);
        $showItemId = isset($param[0]['id']) ? $param[0]['id'] : 0 ;
//        $pubimgroot = PUBIMGROOT;
//        $templateName = $config['template_name'];
//        $nullImgSrc = array('pubimgroot'=>$pubimgroot, 'templateName'=>$templateName);
		$promotionTitle = self::getPromotionTitle();//get the promotion title show in product page when shipping_type=1 and free_shipping_status=1
		
		foreach ($arr as $item){
        	//sale price must greater than 0
        	if(empty($item['sale_price']) || $item['sale_price'] <= 0 || empty($item["inventory"]) || $item["inventory"] <= 0 ){
        		//if current item id is default item id then replace item id set 0
        		if($item["id"] == $showItemId) {
        			$showItemId = 0;//if $showItemId is 0 then random set a item id
        		}        		
        		continue;
        	}
        	//random set a item id
			if($showItemId == 0){ //if the $showItemId is 0 , this is the default image is not set in DB , then find the random itemId
				$showItemId = $item['id'];
			}
			$default_src = "";
			$srcsArray = array();
			$arrSrc = $this->getSrcByItemId($item['id']);//get the one item all image info
			if(empty($arrSrc)){//this item do not have image
				$default_src = "//".ConfigConstants::$iConstants.".".DOMAIN."/t/".$config['template_name']."/image/product"."_";
			}else{
                foreach ($arrSrc as $src){
	                if(!empty($src['folder']) && !empty($src['name']) && isset($src['folder']) && isset($src['name'])){
//	                    $srcsArray[] = "//".ConfigConstants::$piConstants.".".DOMAIN."/p".$src['folder'].$src['name']."_";
	                    $srcsArray[] = ConfigConstants::getPrivateImagePath()."/p".$src['folder'].$src['name']."_";
//	                    if($src['default_item'] == 1 && $src['default_item_image'] == 1){
	                    if($src['default_item_image'] == 1){//get the default image
	                        $default_src = ConfigConstants::getPrivateImagePath()."/p".$src['folder'].$src['name']."_";
	                    }//else if do not have default image but have other image then this is a bug.dawei say in 090703
	                }else{
//	                    $srcsArray[] = "//".ConfigConstants::$iConstants.".".DOMAIN."/t/".$config['template_name']."/image/product"."_";
	                    $srcsArray[] = ConfigConstants::getTImagePath()."/product"."_";
	                }
	            }
			}

            $freeShipping = "";//set the promotion title
			if($item['shipping_type'] == 1 && !empty($promotionTitle) && $item['free_shipping_status'] == 1){
            	$freeShipping = $promotionTitle;
            }
	        $attr1 = $item['attribute_1'] == null ? "" : $item['attribute_1'];
	        $attr2 = $item['attribute_2'] == null ? "" : $item['attribute_2'];
	        $attr3 = $item['attribute_3'] == null ? "" : $item['attribute_3'];
	        $attr4 = $item['attribute_4'] == null ? "" : $item['attribute_4'];
	        $attr5 = $item['attribute_5'] == null ? "" : $item['attribute_5'];
	        $value1 = $item['value_1'] == null ? "" : $item['value_1'];
	        $value2 = $item['value_2'] == null ? "" : $item['value_2'];
	        $value3 = $item['value_3'] == null ? "" : $item['value_3'];
	        $value4 = $item['value_4'] == null ? "" : $item['value_4'];
	        $value5 = $item['value_5'] == null ? "" : $item['value_5'];
	        //akimimi////////////////////////////////////////////
	        $key1 = $item['key_1'] == null ? "" : $item['key_1'];
	        $key2 = $item['key_2'] == null ? "" : $item['key_2'];
	        $key3 = $item['key_3'] == null ? "" : $item['key_3'];
	        $key4 = $item['key_4'] == null ? "" : $item['key_4'];
	        $key5 = $item['key_5'] == null ? "" : $item['key_5'];
	        if($isMobile){//是手机只生成第一个属性
	        	$metadata1 = $item['metadata_1'] == null ? "" : $item['metadata_1'];
	            $metadata2 = null;
	            $metadata3 = null;
	            $metadata4 = null;
	            $metadata5 = null;
	        }else{
	        	$metadata1 = $item['metadata_1'] == null ? "" : $item['metadata_1'];
	            $metadata2 = $item['metadata_2'] == null ? "" : $item['metadata_2'];
	            $metadata3 = $item['metadata_3'] == null ? "" : $item['metadata_3'];
	            $metadata4 = $item['metadata_4'] == null ? "" : $item['metadata_4'];
	            $metadata5 = $item['metadata_5'] == null ? "" : $item['metadata_5'];
	        }
	        
	        //attribute1
	        if(!empty($metadata1)){
		        $isOptionType1 = self::isOptionType($metadata1);
	            $attribute1Title = "";
	            $attribute1ColorPath = "";
	            if(self::isColorAttribute($metadata1)){
	                $result1 = self::getAttributeValueTitleAndPath($metadata1, $key1);
	                $attr1 = $result1["metadata_title"];
	                $attribute1ColorPath = $result1["path"];
	                if($isOptionType1){//true is 3 or 6
	                    $value1 = $key1;
	                    $attribute1Title = $result1["metadata_options_title"];
	                }else{//false is use item table value
	                    $attribute1Title = $value1;
	                }
	            }else{
	                $result1 = self::getAttributeValueAndTitle($metadata1, $key1);
	                $attr1 = $result1["metadata_title"];
	                if($isOptionType1){//true is 3 or 6
	                    $value1 = $key1;
	                    $attribute1Title = $result1["metadata_options_title"];
	                }else{
	                    $attribute1Title = $value1;
	                }
	            }
	        }else{
	        	$attr1 = "";
	        	$value1 = "";
	        	$attribute1ColorPath = "";
	        	$attribute1Title = "";
	        }
	        //attribute2
	        if(!empty($metadata2)){
		        $result2 = self::getAttributeValueAndTitle($metadata2, $key2);
		        $attr2 = $result2["metadata_title"];
	            if(self::isOptionType($metadata2)){//true is 3 or 6
	            	$value2 = $key2;
	                $attribute2Title = $result2["metadata_options_title"];
	            }else{
	            	$attribute2Title = $value2;
	            }
	        }else{
	        	$attr2 = "";
                $value2 = "";
                $attribute2Title = "";
	        }
	        //attribute3
	        if(!empty($metadata3)){
		        $result3 = self::getAttributeValueAndTitle($metadata3, $key3);
		        $attr3 = $result3["metadata_title"];
	            if(self::isOptionType($metadata3)){//true is 3 or 6
	            	$value3 = $key3;
	                $attribute3Title = $result3["metadata_options_title"];
	            }else{
	            	$attribute3Title = $value3;
	            }
	        }else{
	        	$attr3 = "";
                $value3 = "";
                $attribute3Title = "";
	        }
	        //attribute4
	        if(!empty($metadata4)){
		        $result4 = self::getAttributeValueAndTitle($metadata4, $key4);
		        $attr4 = $result4["metadata_title"];
	            if(self::isOptionType($metadata4)){//true is 3 or 6
	            	$value4 = $key4;
	                $attribute4Title = $result4["metadata_options_title"];
	            }else{
	            	$attribute4Title = $value4;
	            }
	        }else{
                $attr4 = "";
                $value4 = "";
                $attribute4Title = "";
	        }
	        //attribute5
	        if(!empty($metadata5)){
		        $result5 = self::getAttributeValueAndTitle($metadata5, $key5);
		        $attr5 = $result5["metadata_title"];
	            if(self::isOptionType($metadata5)){//true is 3 or 6
	            	$value5 = $key5;
	                $attribute5Title = $result5["metadata_options_title"];
	            }else{
	            	$attribute5Title = $value5;
	            }
	        }else{
	        	$attr5 = "";
                $value5 = "";
                $attribute5Title = "";
	        }
	        ///////////////////////////////////////////////////////////////////////
	        $attrNum = $attr1 != "" ? 1 : $attrNum;
	        $attrNum = $attr2 != "" ? 2 : $attrNum;
	        $attrNum = $attr3 != "" ? 3 : $attrNum;
	        $attrNum = $attr4 != "" ? 4 : $attrNum;
	        $attrNum = $attr5 != "" ? 5 : $attrNum;

	        if($item['shipping_type'] == 1){
	        	$shippingType = "NORMAL";
	        }elseif($item['shipping_type'] == 2){
	        	$shippingType = "FIX_RATE";
	        }else{
	        	$shippingType = "NO_SHIPPING";
	        }
	        if(intval($item['inventory']) > 0){
	        	$inventory = true;
	        }else{
	        	$inventory = false;
	        }
	        if($shippingType == "FIX_RATE"){
	            $pitems["item_".$item['id']] = array('id'=>$item['id'],
	                                            'product_id'=>$item['product_id'],
	                                            'list_price'=>$item['list_price'],
	                                            'sale_price'=>$item['sale_price'],
                                                'market_price'=>$item['marketing_price'],

	                                            'attribute_1'=>$attr1,
	                                            'attribute_1_title'=>$attribute1Title,
	                                            'attribute_1_color_path'=>$attribute1ColorPath,
	                                            'value_1'=>$value1,

	                                            'attribute_2'=>$attr2,
	                                            'attribute_2_title'=>$attribute2Title,
	                                            'value_2'=>$value2,

	                                            'attribute_3'=>$attr3,
	                                            'attribute_3_title'=>$attribute3Title,
	                                            'value_3'=>$value3,

	                                            'attribute_4'=>$attr4,
	                                            'attribute_4_title'=>$attribute4Title,
	                                            'value_4'=>$value4,

	                                            'attribute_5'=>$attr5,
	                                            'attribute_5_title'=>$attribute5Title,
	                                            'value_5'=>$value5,

	                                            'shipping_type'=>$shippingType,
	                                            'upc'=>$item['upc'],
	                                            'sku'=>$item['sku'],
	                                            'shipping_fee'=>$item['weight'],
	                                            'inventory'=>$inventory,

	                                            'main_image_url'=>$default_src,
	                                            'image_url'=>$srcsArray,
	                                            'free_shipping' => $freeShipping,
	                                            'attr_num' => $attrNum
	                                             );
	        }else{
	            $pitems["item_".$item['id']] = array('id'=>$item['id'],
	                                            'product_id'=>$item['product_id'],
	                                            'list_price'=>$item['list_price'],
	                                            'sale_price'=>$item['sale_price'],
                                                'market_price'=>$item['marketing_price'],

                                                'attribute_1'=>$attr1,
                                                'attribute_1_title'=>$attribute1Title,
                                                'attribute_1_color_path'=>$attribute1ColorPath,
                                                'value_1'=>$value1,

                                                'attribute_2'=>$attr2,
                                                'attribute_2_title'=>$attribute2Title,
                                                'value_2'=>$value2,

                                                'attribute_3'=>$attr3,
                                                'attribute_3_title'=>$attribute3Title,
                                                'value_3'=>$value3,

                                                'attribute_4'=>$attr4,
                                                'attribute_4_title'=>$attribute4Title,
                                                'value_4'=>$value4,

                                                'attribute_5'=>$attr5,
                                                'attribute_5_title'=>$attribute5Title,
                                                'value_5'=>$value5,

	                                            'shipping_type'=>$shippingType,
	                                            'upc'=>$item['upc'],
	                                            'sku'=>$item['sku'],
	                                            'weight'=>$item['weight'],
	                                            'inventory'=>$inventory,

	                                            'main_image_url'=>$default_src,
	                                            'image_url'=>$srcsArray,
	                                            'free_shipping' => $freeShipping,
                                                'attr_num' => $attrNum
	                                             );
	        }
		}

		$pitems['show_itemid'] = array($showItemId);
		//$pitems['nullImgSrc'] = $nullImgSrc;
		$pitems['productTitle'] = $productTitle;//use in js function show in picture list alt attribute
		$pitems['description'] = $arrProduct[0]['description'];
		$pitems['urlTitle'] = $arrProduct[0]['url_title'];
		$pitems['related1'] = $arrProduct[0]['related_1'];
		$pitems['related2'] = $arrProduct[0]['related_2'];
		$pitems['related3'] = $arrProduct[0]['related_3'];
		$pitems['related4'] = $arrProduct[0]['related_4'];
		$pitems['seoTitle'] = $arrProduct[0]['seo_title'];
		$pitems['seoKeyword'] = $arrProduct[0]['seo_keyword'];
		$pitems['seoDescription'] = $arrProduct[0]['seo_description'];
		$pitems['attr_num'] = $attrNum;
		return $pitems;
	}

	public function getProductByUrlTitleAndItemId($productUrlTitle, $itemId, $isMobile=false){   //give the product url title and item id//used in js
        $config = $GLOBALS["config"];
        $attrNum = 0;//count how many effective attrbute
		$arrProduct = $this->productDao->getByUrlTitle($productUrlTitle);
		$pid = $arrProduct[0]['id'];
		$productTitle = $arrProduct[0]['title'];
		$arr = $this->productItemDao->getByProductId($pid);
		$pitems = array();
//        $param = $this->getDefaultItemByPid($pid);
//        $showItemId = isset($param[0]['id']) ? $param[0]['id'] : 0 ;
        $showItemId = $itemId;
		$promotionTitle = self::getPromotionTitle();//get the promotion title show in product page when shipping_type=1 and free_shipping_status=1
        foreach ($arr as $item){
        	//sale price must greater than 0
            if(empty($item['sale_price']) || $item['sale_price'] <= 0 || empty($item["inventory"]) || $item["inventory"] <= 0 ){
                //if current item id is default item id then replace item id set 0
                if($item["id"] == $showItemId) {
                    $showItemId = 0;//if $showItemId is 0 then random set a item id
                } 
            	continue;
            }
            //        	
			if($showItemId == 0){ //if the $showItemId is 0 , this is the default item is not set in DB , then find the random itemId
				$showItemId = $item['id'];
			}
			$default_src = "";
			$srcsArray = array();
			$arrSrc = $this->getSrcByItemId($item['id']);//get the one item all image info
			if(empty($arrSrc)){//this item do not have image
				$default_src = "//".ConfigConstants::$iConstants.".".DOMAIN."/t/".$config['template_name']."/image/product"."_";
			}else{
                foreach ($arrSrc as $src){
	                if(!empty($src['folder']) && !empty($src['name']) && isset($src['folder']) && isset($src['name'])){
//	                    $srcsArray[] = "//".ConfigConstants::$piConstants.".".DOMAIN."/p".$src['folder'].$src['name']."_";
	                    $srcsArray[] = ConfigConstants::getPrivateImagePath()."/p".$src['folder'].$src['name']."_";
//	                    if($src['default_item'] == 1 && $src['default_item_image'] == 1){
	                    if($src['default_item_image'] == 1){//get the default image
	                        $default_src = ConfigConstants::getPrivateImagePath()."/p".$src['folder'].$src['name']."_";
	                    }//else if do not have default image but have other image then this is a bug.dawei say in 090703
	                }else{
//	                    $srcsArray[] = "//".ConfigConstants::$iConstants.".".DOMAIN."/t/".$config['template_name']."/image/product"."_";
	                    $srcsArray[] = ConfigConstants::getTImagePath()."/product"."_";
	                }
	            }
			}

//			$value1[] = $item['value_1'];
//			$value2[] = $item['value_2'];
//			$value3[] = $item['value_3'];
//			$value4[] = $item['value_4'];
//			$value5[] = $item['value_5'];
            $freeShipping = "";//set the promotion title
			if($item['shipping_type'] == 1 && !empty($promotionTitle) && $item['free_shipping_status'] == 1){
            	$freeShipping = $promotionTitle;
            }
	        $attr1 = $item['attribute_1'] == null ? "" : $item['attribute_1'];
	        $attr2 = $item['attribute_2'] == null ? "" : $item['attribute_2'];
	        $attr3 = $item['attribute_3'] == null ? "" : $item['attribute_3'];
	        $attr4 = $item['attribute_4'] == null ? "" : $item['attribute_4'];
	        $attr5 = $item['attribute_5'] == null ? "" : $item['attribute_5'];
	        $value1 = $item['value_1'] == null ? "" : $item['value_1'];
	        $value2 = $item['value_2'] == null ? "" : $item['value_2'];
	        $value3 = $item['value_3'] == null ? "" : $item['value_3'];
	        $value4 = $item['value_4'] == null ? "" : $item['value_4'];
	        $value5 = $item['value_5'] == null ? "" : $item['value_5'];
            //akimimi////////////////////////////////////////////
            $key1 = $item['key_1'] == null ? "" : $item['key_1'];
            $key2 = $item['key_2'] == null ? "" : $item['key_2'];
            $key3 = $item['key_3'] == null ? "" : $item['key_3'];
            $key4 = $item['key_4'] == null ? "" : $item['key_4'];
            $key5 = $item['key_5'] == null ? "" : $item['key_5'];
            if($isMobile){//是手机只生成第一个属性
                $metadata1 = $item['metadata_1'] == null ? "" : $item['metadata_1'];
                $metadata2 = null;
                $metadata3 = null;
                $metadata4 = null;
                $metadata5 = null;
            }else{
                $metadata1 = $item['metadata_1'] == null ? "" : $item['metadata_1'];
                $metadata2 = $item['metadata_2'] == null ? "" : $item['metadata_2'];
                $metadata3 = $item['metadata_3'] == null ? "" : $item['metadata_3'];
                $metadata4 = $item['metadata_4'] == null ? "" : $item['metadata_4'];
                $metadata5 = $item['metadata_5'] == null ? "" : $item['metadata_5'];
            }
            //attribute1
            if(!empty($metadata1)){
                $isOptionType1 = self::isOptionType($metadata1);
                $attribute1Title = "";
                $attribute1ColorPath = "";
                if(self::isColorAttribute($metadata1)){
                    $result1 = self::getAttributeValueTitleAndPath($metadata1, $key1);
                    $attr1 = $result1["metadata_title"];
                    $attribute1ColorPath = $result1["path"];
                    if($isOptionType1){//true is 3 or 6
                        $value1 = $key1;
                        $attribute1Title = $result1["metadata_options_title"];
                    }else{//false is use item table value
                        $attribute1Title = $value1;
                    }
                }else{
                    $result1 = self::getAttributeValueAndTitle($metadata1, $key1);
                    $attr1 = $result1["metadata_title"];
                    if($isOptionType1){//true is 3 or 6
                        $value1 = $key1;
                        $attribute1Title = $result1["metadata_options_title"];
                    }else{
                        $attribute1Title = $value1;
                    }
                }
            }else{
                $attr1 = "";
                $value1 = "";
                $attribute1ColorPath = "";
                $attribute1Title = "";
            }

            //attribute2
            if(!empty($metadata2)){
                $result2 = self::getAttributeValueAndTitle($metadata2, $key2);
                $attr2 = $result2["metadata_title"];
                if(self::isOptionType($metadata2)){//true is 3 or 6
                    $value2 = $key2;
                    $attribute2Title = $result2["metadata_options_title"];
                }else{
                    $attribute2Title = $value2;
                }
            }else{
                $attr2 = "";
                $value2 = "";
                $attribute2Title = "";
            }
            //attribute3
            if(!empty($metadata3)){
                $result3 = self::getAttributeValueAndTitle($metadata3, $key3);
                $attr3 = $result3["metadata_title"];
                if(self::isOptionType($metadata3)){//true is 3 or 6
                    $value3 = $key3;
                    $attribute3Title = $result3["metadata_options_title"];
                }else{
                    $attribute3Title = $value3;
                }
            }else{
                $attr3 = "";
                $value3 = "";
                $attribute3Title = "";
            }
            //attribute4
            if(!empty($metadata4)){
                $result4 = self::getAttributeValueAndTitle($metadata4, $key4);
                $attr4 = $result4["metadata_title"];
                if(self::isOptionType($metadata4)){//true is 3 or 6
                    $value4 = $key4;
                    $attribute4Title = $result4["metadata_options_title"];
                }else{
                    $attribute4Title = $value4;
                }
            }else{
                $attr4 = "";
                $value4 = "";
                $attribute4Title = "";
            }
            //attribute5
            if(!empty($metadata5)){
                $result5 = self::getAttributeValueAndTitle($metadata5, $key5);
                $attr5 = $result5["metadata_title"];
                if(self::isOptionType($metadata5)){//true is 3 or 6
                    $value5 = $key5;
                    $attribute5Title = $result5["metadata_options_title"];
                }else{
                    $attribute5Title = $value5;
                }
            }else{
                $attr5 = "";
                $value5 = "";
                $attribute5Title = "";
            }
            ///////////////////////////////////////////////////////////////////////
            $attrNum = $attr1 != "" ? 1 : $attrNum;
            $attrNum = $attr2 != "" ? 2 : $attrNum;
            $attrNum = $attr3 != "" ? 3 : $attrNum;
            $attrNum = $attr4 != "" ? 4 : $attrNum;
            $attrNum = $attr5 != "" ? 5 : $attrNum;

	        if($item['shipping_type'] == 1){
	        	$shippingType = "NORMAL";
	        }elseif($item['shipping_type'] == 2){
	        	$shippingType = "FIX_RATE";
	        }else{
	        	$shippingType = "NO_SHIPPING";
	        }
	        if(intval($item['inventory']) > 0){
	        	$inventory = true;
	        }else{
	        	$inventory = false;
	        }
	        if($shippingType == "FIX_RATE"){
	            $pitems["item_".$item['id']] = array('id'=>$item['id'],
	                                            'product_id'=>$item['product_id'],
	                                            'list_price'=>$item['list_price'],
	                                            'sale_price'=>$item['sale_price'],
                                                'market_price'=>$item['marketing_price'],


                                                'attribute_1'=>$attr1,
                                                'attribute_1_title'=>$attribute1Title,
                                                'attribute_1_color_path'=>$attribute1ColorPath,
                                                'value_1'=>$value1,

                                                'attribute_2'=>$attr2,
                                                'attribute_2_title'=>$attribute2Title,
                                                'value_2'=>$value2,

                                                'attribute_3'=>$attr3,
                                                'attribute_3_title'=>$attribute3Title,
                                                'value_3'=>$value3,

                                                'attribute_4'=>$attr4,
                                                'attribute_4_title'=>$attribute4Title,
                                                'value_4'=>$value4,

                                                'attribute_5'=>$attr5,
                                                'attribute_5_title'=>$attribute5Title,
                                                'value_5'=>$value5,


	                                            'shipping_type'=>$shippingType,
	                                            'upc'=>$item['upc'],
	                                            'sku'=>$item['sku'],
	                                            'shipping_fee'=>$item['weight'],
	                                            'inventory'=>$inventory,

	                                            'main_image_url'=>$default_src,
	                                            'image_url'=>$srcsArray,
	                                            'free_shipping' => $freeShipping,
                                                'attr_num' => $attrNum
	                                             );
	        }else{
	            $pitems["item_".$item['id']] = array('id'=>$item['id'],
	                                            'product_id'=>$item['product_id'],
	                                            'list_price'=>$item['list_price'],
	                                            'sale_price'=>$item['sale_price'],
                                                'market_price'=>$item['marketing_price'],


                                                'attribute_1'=>$attr1,
                                                'attribute_1_title'=>$attribute1Title,
                                                'attribute_1_color_path'=>$attribute1ColorPath,
                                                'value_1'=>$value1,

                                                'attribute_2'=>$attr2,
                                                'attribute_2_title'=>$attribute2Title,
                                                'value_2'=>$value2,

                                                'attribute_3'=>$attr3,
                                                'attribute_3_title'=>$attribute3Title,
                                                'value_3'=>$value3,

                                                'attribute_4'=>$attr4,
                                                'attribute_4_title'=>$attribute4Title,
                                                'value_4'=>$value4,

                                                'attribute_5'=>$attr5,
                                                'attribute_5_title'=>$attribute5Title,
                                                'value_5'=>$value5,


	                                            'shipping_type'=>$shippingType,
	                                            'upc'=>$item['upc'],
	                                            'sku'=>$item['sku'],
	                                            'weight'=>$item['weight'],
	                                            'inventory'=>$inventory,

	                                            'main_image_url'=>$default_src,
	                                            'image_url'=>$srcsArray,
	                                            'free_shipping' => $freeShipping,
                                                'attr_num' => $attrNum
	                                             );
	        }

		}

//		$attr1 = array($distinctAttributes[0]['attribute_1']=>$value1);
//		$attr2 = array($distinctAttributes[0]['attribute_2']=>$value2);
//		$attr3 = array($distinctAttributes[0]['attribute_3']=>$value3);
//		$attr4 = array($distinctAttributes[0]['attribute_4']=>$value4);
//		$attr5 = array($distinctAttributes[0]['attribute_5']=>$value5);
//		$pitems['attr1'] = array($attr1);
//		$pitems['attr2'] = array($attr2);
//		$pitems['attr3'] = array($attr3);
//		$pitems['attr4'] = array($attr4);
//		$pitems['attr5'] = array($attr5);

		$pitems['show_itemid'] = array($showItemId);
		//$pitems['nullImgSrc'] = $nullImgSrc;
		$pitems['productTitle'] = $productTitle;//use in js function show in picture list alt attribute
		$pitems['description'] = $arrProduct[0]['description'];
		$pitems['urlTitle'] = $arrProduct[0]['url_title'];
		$pitems['related1'] = $arrProduct[0]['related_1'];
		$pitems['related2'] = $arrProduct[0]['related_2'];
		$pitems['related3'] = $arrProduct[0]['related_3'];
		$pitems['related4'] = $arrProduct[0]['related_4'];
		$pitems['seoTitle'] = $arrProduct[0]['seo_title'];
		$pitems['seoKeyword'] = $arrProduct[0]['seo_keyword'];
		$pitems['seoDescription'] = $arrProduct[0]['seo_description'];
		$pitems['attr_num'] = $attrNum;
		return $pitems;
	}

	public function getSrcByItemId($itemId){   //used in getProductByUrlTitle function //set data in js
		$param = $this->productImageDao->getByItemId($itemId);
        return $param;
	}

	public function getProductInProgramByUrlTitle($productUrlTitle){   // not used  in js
        $arr = $this->productDao->getByUrlTitle($productUrlTitle);
        if(empty($arr)){
        	HttpUtil::renderError();
        }

//        $productId = $arr[0]['id']; //modify in 0701
		//get product by item//product table info //not used in js array
//        $product = $this->getProductById($productId); //modify in 0701
        $product = $this->getProductInfo($arr);
        $result[1] = array('product' => $product);  //because this type used in the productView file(create before 5.1)
        return $result;
	}

	public function getDefaultItemByPid($pid){
		$result = array();
		$result = $this->productItemDao->getDefaultItemByProductId($pid);
		return $result;
	}

	public function getDefaultItemIdByPid($pid){
		$result = array();
		$result = $this->productItemDao->getDefaultItemByProductId($pid);
		$defaultItemId = $result[0]['item_id'];
		return $defaultItemId;
	}

	///////////////////////////////////////////////before 5.1
//	public function getProductById($productId){     //used in  getByProductItemId function
	public function getProductInfo($result){      //modify in 0701
//        $result = $this->productDao->getByProductId($productId);      //modify in 0701
        $productId = $result[0]['id'];
        $title = $result[0]['title'];
        $urlTitle = $result[0]['url_title'];
        $description = $result[0]['description'];
        $related1 = $result[0]['related_1'];
        $related2 = $result[0]['related_2'];
        $related3 = $result[0]['related_3'];
        $related4 = $result[0]['related_4'];
        $seoTitle = $result[0]['seo_title'];
        $seoKeyword = $result[0]['seo_keyword'];
        $seoDesc = $result[0]['seo_description'];
        $result = array('product_id'=>$productId,
                        'title'=>$title,
                        'url_title'=>$urlTitle,
                        'description'=>$description,
                        'related_1'=>$related1,
                        'related_2'=>$related2,
                        'related_3'=>$related3,
                        'related_4'=>$related4,
                        'seo_title'=>$seoTitle,
                        'seo_keyword'=>$seoKeyword,
                        'seo_desc'=>$seoDesc);
        return $result;
	}

	public function getOtherProductItemsById($productId, $itemId){     //used in  getByProductItemId function
        $result = $this->productItemDao->getOtherByPid($productId, $itemId);    //second through the pid and exclude the item id get the product item info
        $arr = array();
        for($i = 0; $i < count($result); $i++){
        	$arr[] = $result[$i]['id'];
        }
        return $arr;
	}

	public function getDefaultItemImgByItemId($itemId){   //used in product view//get the product image and default==1
		$result = array();
		$result = $this->productImageDao->getDefaultImageByItemId($itemId);
		return $result;
	}

	public function getItemImgByItemId($itemId){   //get the product image  //show the piclit
        $result = array();
        $result = $this->productImageDao->getByItemId($itemId);
        return $result;
	}

	public function getOtherItemByPidAndItemId($pid, $itemId){
		$reault = array();
		$arr = $this->productItemDao->getOtherByPid($pid, $itemId);
		foreach ($arr as $item) { //get the item id to find the default item image
			$itemId = $item['id'];
			$reault[] = $this->productImageDao->getDefaultImageByItemId($itemId);
		}
		return $reault;
	}

	public function getItemInfoByItemId($itemId){
        $param = $this->productItemDao->getById($itemId);
        return $param;
	}

	/**
	 * get the promotion title from promotion table to show in product page when shipping_type = 1
	 */
	public function getPromotionTitle(){
		$result = $this->promotionDao->getPromotionInfo();
		$promotionTitle = isset($result[0]['name']) ? $result[0]['name'] : "";
		return $promotionTitle;
	}


//	akimimi
	/**
	 * give the item id to get the metadata group name(use in the product top find the is primary=1)
	 */
    public function getGroupByItemId($itemId){
        $result = $this->metadataGroupDao->getGroupByItemId($itemId);
        return $result;
    }

	/**
	 * give the item id to get the query and reply and customer name
	 */
	public function getQueryInfoByItemId($pId){
		$result = $this->queryDao->getQueryInfoByItemId($pId);
        return $result;
	}

	/**
	 * judge current all attribute type if is 3 or 6
	 * return boolean
	 */
	public function isOptionType($metadataId){
		$metadata = $this->metadataDao->getMetadataById($metadataId);
		$metadataType = $metadata[0]["input_type"];
		if($metadataType == 3 || $metadataType == 6){//3 or 6 is dropDown or option
			return true;
		}else{
			return false;
		}
	}

	/**
	 * judge the attribute_1 if is color
	 * return boolean
	 */
	public function isColorAttribute($metadataId){
        $metadata = $this->metadataDao->getMetadataById($metadataId);
        $metadataTitle = $metadata[0]["title"];
        $findTitle = "颜色";
        $pos = strpos($metadataTitle, $findTitle);
        if ($pos === false) {
            return false;
        }else{
        	return true;
        }
	}

	/**
	 * get the attribute and value and title
	 * use in attribute 1
	 * $metadataId is metadata_1 , $metadataOptionId is key_1
	 */
	public function getAttributeValueTitleAndPath($metadataId, $metadataOptionId){//$metadataKey is not 0 or null
        $result = array();
		$metadata = $this->metadataDao->getMetadataById($metadataId);
		$metadataTitle = $metadata[0]["title"];//show the color title
        $metadataOptions = $this->metadataOptionDao->getMetadataOptionsById($metadataOptionId);
        $metadataOptionsTitle = $metadataOptions[0]["title"];
        $metadataOptionsValue = $metadataOptions[0]["value"];
		$color = $GLOBALS["color"];
		$colorPath = $color[$metadataOptionsValue];
		$result["metadata_title"] = $metadataTitle;
		$result["metadata_options_title"] = $metadataOptionsTitle;
		$result["path"] = $colorPath;
		return $result;
	}

	/**
	 * get the attribute and value and title
	 * use in attribute 2-5
	 */
	public function getAttributeValueAndTitle($metadataId, $metadataOptionId){//$metadataKey is not 0 or null
        $result = array();
		$metadata = $this->metadataDao->getMetadataById($metadataId);
		$metadataTitle = $metadata[0]["title"];
		$metadataOptions = $this->metadataOptionDao->getMetadataOptionsById($metadataOptionId);
		$metadataOptionsTitle = $metadataOptions[0]["title"];
		$result["metadata_title"] = $metadataTitle;
		$result["metadata_options_title"] = $metadataOptionsTitle;
		return $result;
	}

	public function getMetadataInfoByItemId($itemId){
		$data = array();//show the group title and metadata info
		$dataShort = array();//only show the metadata info
		$groupInfo = self::getGroupByItemId($itemId);
        foreach ($groupInfo as $item) {//$item["id"] is group id
            $data[$item["id"]] = array(
                                      "title" => $item["title"],
                                      "metadatas" => array()
                                 );
        }
		$metadata = array();
		$metadataInfo = $this->itemMetadataDao->getMetadataInputTypeByItemId($itemId);
		foreach ($metadataInfo as $item) {
		    $metadataPrimary = array();
			if($item["input_type"] == 3 || $item["input_type"] == 6){//3=下拉框   6=单选
				$metadataOptionResult = $this->metadataOptionDao->getMetadataOptionsById($item["value"]);
				$metadata = array("attribute" => $item["title"], "title" => $metadataOptionResult[0]["title"]);
				if($item["is_primary"] == 1){
					$metadataPrimary = array("attribute" => $item["title"], "title" => $metadataOptionResult[0]["title"]);
				}
			}elseif($item["input_type"] == 1 || $item["input_type"] == 5){
				$metadata = array("attribute" => $item["title"], "title" => $item["value"]);
                if($item["is_primary"] == 1){
                    $metadataPrimary = array("attribute" => $item["title"], "title" => $item["value"]);
                }
			}elseif($item["input_type"] == 2){
				$metadata = array("attribute" => $item["title"], "title" => $item["desc_value"]);
                if($item["is_primary"] == 1){
                    $metadataPrimary = array("attribute" => $item["title"], "title" => $item["desc_value"]);
                }
			}elseif($item["input_type"] == 4){//多选框，会有多个结果
			    $metadataOptionResult = $this->metadataOptionDao->getMetadataOptionsById($item["value"]);
                $metadata = array("attribute" => $item["title"], "title" => $metadataOptionResult[0]["title"]);
                if($item["is_primary"] == 1){
                    $metadataPrimary = array("attribute" => $item["title"], "title" => $metadataOptionResult[0]["title"]);
                }
			}
			$data[$item["group_id"]]["metadatas"][] = $metadata;
			if(!empty($metadataPrimary)){
				$dataShort[] = $metadataPrimary;
			}
		}
        $param = array("data" => $data, "short_data" => $dataShort);
        return $param;
	}

	public function getRandomProduct(){
	    $arr_result = $this->productDao->getRandomProduct();
	    return $arr_result[0];
	}

	/**
	 * generate the konwledge html whole page by category url title
	 */
	public static function generateBuyKnowledgeByCategoryUrlTitle($categoryUrlTitle){
		if( $categoryUrlTitle == "men_costume" || $categoryUrlTitle == "women_costume" ){//服装
			return BuyKnowledge::getCosView();
		}elseif($categoryUrlTitle == "dc_and_accessories"){//数码相机
            return BuyKnowledge::getDicView();
		}elseif($categoryUrlTitle == "household"){//家电
            return BuyKnowledge::getJDView();
		}elseif($categoryUrlTitle == "makeup"){//化妆品
			return BuyKnowledge::getMakView();
		}elseif($categoryUrlTitle == "mobile_and_accessories"){//手机
			return BuyKnowledge::getMobileView();
		}elseif($categoryUrlTitle == "digital_player"){//数码影音
			return BuyKnowledge::getMP3View();
		}elseif($categoryUrlTitle == "notebook_and_accessories"){//笔记本
			return BuyKnowledge::getNoteBookView();
		}elseif($categoryUrlTitle == "peripherals"){//电脑配件
			return BuyKnowledge::getPJView();
		}elseif($categoryUrlTitle == "sports"){//体育用品
			return BuyKnowledge::getSpoView();
		}elseif($categoryUrlTitle == "gift_trinket"){//饰品
			return BuyKnowledge::getTriView();
		}else{
			return "";
		}

	}

	/**
	 * generate the konwledge html whole page by category url title
	 */
	public static function generateBuyTopPrimaryKnowledgeByCategoryUrlTitle($categoryUrlTitle){
		$topPrimaryKnowledge = "";
//        if( $categoryUrlTitle == "men_costume" || $categoryUrlTitle == "women_costume" ){//服装
//          $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('cos_01');\">[清洁储藏]</p>";
//      
//      }elseif($categoryUrlTitle == "dc_and_accessories"){//数码相机
//            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('dic_01');\">[行货/水货-区分]</p>";
//            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('dic_01');\">[翻新机-鉴别]</p>";
//            
//      }elseif($categoryUrlTitle == "household"){//家电
//            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('ntb_01');\">[注意事项]</p>";
//            
//      }elseif($categoryUrlTitle == "makeup"){//化妆品
//            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('mak_01');\">[正品识别]</p>";
//          
//      }elseif($categoryUrlTitle == "mobile_and_accessories"){//手机
//          $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('mob_01');\">[行货/水货-区分]</p>";
//            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('mob_01');\">[定制/非定制]</p>";
//            
//      }elseif($categoryUrlTitle == "digital_player"){//数码影音
//            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('mp3_01');\">[行货/水货-区分]</p>";
//            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('mp3_01');\">[数码保养]</p>";
//          
////        }elseif($categoryUrlTitle == "notebook_and_n_accessories"){//笔记本
//      }elseif($categoryUrlTitle == "notebook_and_accessories"){//笔记本
//            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('ntb_01');\">[行货/水货-区分]</p>";
//            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('ntb_01');\">[本本保养]</p>";
//          
//      }elseif($categoryUrlTitle == "peripherals"){//电脑配件
//            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('pc_01');\">[内存-米米贴士]</p>";
//            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('pc_01');\">[硬盘-米米贴士]</p>";
//          
//      }elseif($categoryUrlTitle == "sports"){//体育用品
//            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('mot_01');\">[使用技巧]</p>";
//          
//      }elseif($categoryUrlTitle == "gift_trinket"){//饰品
//            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('tri_01');\">[银饰鉴别]</p>";
//            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('tri_01');\">[宝石分类]</p>";
		
		if( $categoryUrlTitle == "men_costume" || $categoryUrlTitle == "women_costume" ){//服装
            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('cs1');\">[清洁储藏]</p>";
        
        }elseif($categoryUrlTitle == "dc_and_accessories"){//数码相机
            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('hsqb');\">[行货/水货-区分]</p>";
            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('fxjjb');\">[翻新机-鉴别]</p>";
            
        }elseif($categoryUrlTitle == "household"){//家电
            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('cs1');\">[注意事项]</p>";
            
        }elseif($categoryUrlTitle == "makeup"){//化妆品
            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('cs1');\">[正品识别]</p>";
            
        }elseif($categoryUrlTitle == "mobile_and_accessories"){//手机
            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('hsqb');\">[行货/水货-区分]</p>";
            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('dzjqb');\">[定制/非定制]</p>";
            
        }elseif($categoryUrlTitle == "digital_player"){//数码影音
            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('cs2');\">[行货/水货-区分]</p>";
            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('cs3');\">[数码保养]</p>";
                                      
        }elseif($categoryUrlTitle == "notebook_and_accessories"){//笔记本
            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('hhhsh');\">[行货/水货-区分]</p>";
            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('bycs');\">[本本保养]</p>";
            
        }elseif($categoryUrlTitle == "peripherals"){//电脑配件
            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('cs1');\">[内存-米米贴士]</p>";
            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('cs2');\">[硬盘-米米贴士]</p>";
            
        }elseif($categoryUrlTitle == "sports"){//体育用品
            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('cs1');\">[使用技巧]</p>";
            
        }elseif($categoryUrlTitle == "gift_trinket"){//饰品
            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('cs1');\">[银饰鉴别]</p>";
            $topPrimaryKnowledge .= "<p style='cursor:pointer;' onclick=\"javascript:ProductContent.showBuyKnowledge('cs3');\">[宝石分类]</p>";
            
		}else{
			$topPrimaryKnowledge .= "";
			
		}
        return $topPrimaryKnowledge;
	}

	public function getProductItemsBySearch($int_page,$str_where,$str_sortby,$bool_countOnly=false){
	    $int_limit = Pager::GetPageLimitStartNumber($int_page);
	    $int_pagesize = Pager::GetDefaultPagesize();
	    $arr_results = $this->productItemDao->getProductItemsBySearch($int_limit,$int_pagesize,$str_sortby,$str_where,$bool_countOnly);
	    return $arr_results;
	}
	
	/**
	 * use the pid to get the all item ids split by ','
	 * @return string split by ','
	 */
	public function getItemIdsByProductId($pid){
		$items = "";
		$result = $this->productItemDao->getByProductId($pid);
		foreach ($result as $item) {
			$items .= $item["id"].",";
		}
		$items = substr( $items, 0, ( strlen($items) - 1 ) );
		return $items;
	}
	
	/**
	 * 根据用户id得到用户之前购买的item的信息，并当前产品页面的product id判断当前产品用户之前是否已经购买，没有购买不能发表评论
	 */
	public function isCanOpinion($userId, $pid){
		//得到product下的所有item信息
		$itemIds = $this->productItemDao->getByProductId($pid);
		//得到当前用户买的所有item信息
		$orderItemInfo = $this->orderDao->getOrderItemInfoByUserId($userId);
		//当前用户买的所有item id信息
		$itemIdArray = array();
		foreach ($orderItemInfo as $orderItemIds) {
			$itemIdArray[] = $orderItemIds["item_id"];
		}
		foreach ($itemIds as $itemValue) {
			if( in_array($itemValue["id"] ,$itemIdArray) ){
				return true;
			}
		}
		return false;
	}
	
    /**
     * 兑换礼品，如果礼品是网站中的商品，根据pid显示出产品的信息
     */
	/*
    public function getGiftProductInfo(){   //used in productView
        $result = array();
    	$giftsInfos = $this->giftDao->getAllGiftsByAvailabilityTime();
    	foreach ($giftsInfos as $giftsInfo) {
    		$itemId = $giftsInfo["product_item_id"];
    		if(!empty($itemId)){//礼品是网店中的产品
    			$itemInfos = $this->productItemDao->getById($itemId);
    			$productId = $itemInfos[0]["product_id"];
    			$productInfos = $this->productDao->getGiftProductByPid($productId);
    			$productInfo = $productInfos[0];
    			//生成图片全路径
    			if( !empty($productInfo["folder"]) && !empty($productInfo["name"]) ){
    				$imagePath = ConfigConstants::getProductImagePath($productInfo["folder"], $productInfo["name"], 60);
    			}else{
    				$imagePath = ConfigConstants::getDefaultProductImagePath(60);
    			}
    			
    			$result[] = array(
    			                 "id" => $giftsInfo["id"],
                                 "exhaust_points" => $giftsInfo["exhaust_points"],
                                 "min_ac_points" => $giftsInfo["min_ac_points"],
//                                 "product_item_id" => $giftsInfo["product_item_id"],
                                 "title" => $productInfo["title"],
                                 "description" => "",
                                 "url_title" => $productInfo["url_title"],
                                 "image_path" => $imagePath,
                                 "start_data" => $giftsInfo["start_date"],
                                 "end_data" => $giftsInfo["end_date"]
    			             );
    			
    		}else{//礼品是网店中没有的产品信息都从gifts表中取
    			$result[] = array(
                                 "id" => $giftsInfo["id"],
                                 "exhaust_points" => $giftsInfo["exhaust_points"],
                                 "min_ac_points" => $giftsInfo["min_ac_points"],
//                                 "product_item_id" => $giftsInfo["product_item_id"],
                                 "title" => $giftsInfo["title"],
                                 "description" => $giftsInfo["description"],
                                 "url_title" => "",
                                 "image_path" => $giftsInfo["image_path"],
                                 "start_data" => $giftsInfo["start_date"],
                                 "end_data" => $giftsInfo["end_date"]
                             );
    		}
    	}
        return $result;
    }
*/
	
     /**
     * 查找产品信息用于搜索
     */
    public function getProductByPid($pid){
    	return $this->productDao->getGiftProductByPid($pid);
    }
    
    public function getAllOfSearch($keywords,$cid=0,$brand=null,$lprice=null,$hprice=null){
    	$result = $this->globalSearchDataDao->getAllOfSearch($keywords,$cid,$brand,$lprice,$hprice);
    	return $result;
    }

    public function getCategoryPNumOfSearch($keywords,$cid=0,$brand='',$lprice='',$hprice='',$level){
    	$results = $this->globalSearchDataDao->getCategoryPNumOfSearch($keywords,$cid,$brand,$lprice,$hprice,$level);
    	foreach($results as $result){
    		if($level == "brand"){
    			$group[$result['brand']] = $result['b_num'];
    		} else {
    		    $group[$result['cid']] = $result['p_num'];
    		}
    	}
    	return $group;
    }
    
    public function getProductsBySearch($keywords,$int_nowpage,$cid=0,$brand=null,$lprice=null,$hprice=null){
    	$int_limit = Pager::GetPageLimitStartNumber($int_nowpage);
	    $int_pagesize = Pager::GetDefaultPagesize();
	    return $this->globalSearchDataDao->getProductsBySearch($keywords,$int_limit,$int_pagesize,$cid,$brand,$lprice,$hprice);
    }

}







